package com.hao.epidemic_prevention.security.handler;

import com.hao.epidemic_prevention.utils.JSONResult;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
    private String url;
    private boolean isRedirect;

    public MyAuthenticationSuccessHandler(String url, boolean isRedirect) {
        this.url = url;
        this.isRedirect = isRedirect;
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Authentication authentication) throws IOException, ServletException {
        System.out.println(request.getRemoteHost());
        System.out.println(request.getRemoteAddr());
        System.out.println("自定义认证处理逻辑执行。。。。。。。。。");

        response.setContentType("application/json;charset=utf-8");
        JSONResult result = new JSONResult(200, "登录成功！", null);
        response.getWriter().write(result.toString());

        /*if(isRedirect){
            response.sendRedirect(url);
        }else{
            request.getRequestDispatcher(url).forward(request, response);
        }*/
    }

    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {

    }
}
